package com.pic.mgt.haishin.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.pic.mgt.action.AbstractMissUnitInspectActionImpl;
import com.pic.mgt.action.MessageRecoverAction;
import com.pic.mgt.haishin.manager.terminal.HaishinTerminalManager;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.manager.terminal.BasicTerminalInfoBean;
import com.pic.mgt.monitor.parser.MessageTransactionFileParser;

public class HaishinMissMessageUnitInspectActionImpl extends
								AbstractMissUnitInspectActionImpl {
	
	private HaishinTerminalManager haishinTerminalManager;
	private HaishinMessageRecoverAction haishinMessageRecoverAction;
	
	private String haishinInspectMsgFolderStr;
	private long haishinInspectThreshold;
	private String msgFilenameSuffix; 
	
	public HaishinMissMessageUnitInspectActionImpl() {
		super();
	}

	@Override
	public synchronized boolean inspectMissUnitAndRecover() {
		// ********************** Log **********************
		IgxLog.messageLog.info(this, "Starting miss message inspection process.");
		// ********************** Log **********************
		
		// 取得現在時間
		Date nowDate = new Date();
		
		// 檢測 haishin outbox 目錄下所有端末目錄內的訊息確認檔是否有存在超過指定的時間
		
		SuffixFilenameFilter suffixFilenameFilter = new SuffixFilenameFilter(this.msgFilenameSuffix);
		
		// 取得有效端末清單
		List<BasicTerminalInfoBean> effectiveBasicTerminalInfoBeanList = this.haishinTerminalManager.getEffectiveBasicTerminalInfoBeanList();
		
		for (BasicTerminalInfoBean effectiveBasicTerminalInfoBean : effectiveBasicTerminalInfoBeanList) {
			File[] missMessageFileArray =
					super.checkFileMissUnderFolder(
									new File(this.haishinInspectMsgFolderStr, effectiveBasicTerminalInfoBean.getTerminalId()),
									nowDate,
									this.haishinInspectThreshold,
								    suffixFilenameFilter);
			if (null != missMessageFileArray) {
				for (File missMessageFile : missMessageFileArray) {
					// 如果開啟忽略 0KB 檔案的功能,則要做檢查 (一般 Message 不會開啟此功能)
					if (super.isIgnoreEmptyMsgConfirmationFile) {
						if (missMessageFile.length() <= 0) {
							continue; // 表0KB的確認檔,則忽略之
						}
					}
					
					// ********************** Log **********************
					IgxLog.messageLog.info(this,
									"Miss Message Inspection: detected a miss message '{}'.",
									missMessageFile.getAbsolutePath());
					// ********************** Log **********************
					
					// 進行訊息恢復
					File transactionFile = MessageTransactionFileParser.getTransactionFile(missMessageFile);
					this.haishinMessageRecoverAction.recoverMessage(transactionFile.getName());
				}
			}
		}
		
		// ********************** Log **********************
		IgxLog.messageLog.info(this, "Miss message inspection process completed.");
		// ********************** Log **********************
		
		return true;
	}
	
	// --- getter and setter ---

	public void setHaishinTerminalManager(
			HaishinTerminalManager haishinTerminalManager) {
		this.haishinTerminalManager = haishinTerminalManager;
	}

	public void setHaishinMessageRecoverAction(
			HaishinMessageRecoverAction haishinMessageRecoverAction) {
		this.haishinMessageRecoverAction = haishinMessageRecoverAction;
	}

	public void setHaishinInspectMsgFolderStr(String haishinInspectMsgFolderStr) {
		this.haishinInspectMsgFolderStr = haishinInspectMsgFolderStr;
	}

	public void setHaishinInspectThreshold(long haishinInspectThreshold) {
		this.haishinInspectThreshold = haishinInspectThreshold;
	}

	public void setMsgFilenameSuffix(String msgFilenameSuffix) {
		this.msgFilenameSuffix = msgFilenameSuffix;
	}
}
